package org.example.modules.user.service.Impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.common.utils.ThreadLocalUtil;
import org.example.modules.user.entry.Review;
import org.example.modules.user.mapper.ReviewMapper;
import org.example.modules.user.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description ReviewServiceImpl
 * @Author xiaotian
 * @Date 2025-04-21
 */
@Slf4j
@Service
public class ReviewServiceImpl implements ReviewService {

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public List<Review> getReviewsBySideJobId(String sideJobId) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("side_job_id", sideJobId)
                .eq("status","Q")
                .orderByDesc("create_time");
        return reviewMapper.selectList(queryWrapper).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public void submitReview(JSONObject request) {
        Review review = request.toJavaObject(Review.class);
//        review.setAuthorName(ThreadLocalUtil.getUserId());
        review.setCreatedTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        review.setStatus("Q");
        log.info("review:{}",review);
        reviewMapper.insert(review);
    }

    @Override
    public List<Review> updateReview(JSONObject request) {
        Integer id = request.getInteger("id");
        String status = request.getString("targetStatus");
        reviewMapper.updateStatus(id,status);
        Review review = reviewMapper.selectById(id);
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("side_job_id", review.getSideJobId())
                .orderByDesc("create_time");
        return reviewMapper.selectList(queryWrapper).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    @Override
    public List<Review> getReviewsBySideJobId1(String sideJobId) {
        QueryWrapper<Review> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("side_job_id", sideJobId)
                .orderByDesc("create_time");
        return reviewMapper.selectList(queryWrapper).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }

    private Review convertToDTO(Review entity) {
        Review dto = new Review();
        dto.setId(entity.getId());
        dto.setSideJobId(entity.getSideJobId());
        dto.setAuthorName(entity.getAuthorName());
        dto.setContent(entity.getContent());
        dto.setCreatedTime(entity.getCreatedTime());
        dto.setStatus(entity.getStatus());
        return dto;
    }
}